﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:ItemRequisitionApply
    /// </summary>
    public partial class ItemRequisitionApply
    {
        public ItemRequisitionApply()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply model, List<Model.ItemRequisitionApplyEntry> ItemRequisitionApplyEntryList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_ItemRequisitionApply(");
            strSql.Append("FEntityNo,FApplyDeptNo,FApplyEmplNo,FApplyReason,FApplyDate,FApplyIP,FStatus,FInsEmplNo)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FApplyDeptNo,@FApplyEmplNo,@FApplyReason,@FApplyDate,@FApplyIP,@FStatus,@FInsEmplNo)");
            strSql.Append(";select @returnBId=IDENT_CURRENT('dbo.t_ItemRequisitionApply')");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FApplyDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FApplyEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FApplyReason", SqlDbType.NVarChar,-1),
					new SqlParameter("@FApplyDate", SqlDbType.DateTime),
					new SqlParameter("@FApplyIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FInsEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@returnBId", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FApplyDeptNo;
            parameters[2].Value = model.FApplyEmplNo;
            parameters[3].Value = model.FApplyReason;
            parameters[4].Value = model.FApplyDate;
            parameters[5].Value = model.FApplyIP;
            parameters[6].Value = model.FStatus;
            parameters[7].Value = model.FInsEmplNo;
            parameters[8].Direction = ParameterDirection.Output;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.ItemRequisitionApplyEntry ItemRequisitionApplyEntryModel in ItemRequisitionApplyEntryList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_ItemRequisitionApplyEntry(");
                strSql.Append("FItemRequisitionApplyID,FApplyQty,FItemNo,FEntityNo)");
                strSql.Append(" values (");
                strSql.Append("@FItemRequisitionApplyID,@FApplyQty,@FItemNo,@FEntityNo)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4),
					new SqlParameter("@FApplyQty", SqlDbType.NVarChar,30),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                parameters2[0].Direction = ParameterDirection.InputOutput;
                parameters2[1].Value = ItemRequisitionApplyEntryModel.FApplyQty;
                parameters2[2].Value = ItemRequisitionApplyEntryModel.FItemNo;
                parameters2[3].Value = ItemRequisitionApplyEntryModel.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply model, List<Model.ItemRequisitionApplyEntry> ItemRequisitionApplyEntryList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ItemRequisitionApply set ");
            strSql.Append("FApplyReason=@FApplyReason,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FStatus=@FStatus,");
            strSql.Append("FApplyDeptNo=@FApplyDeptNo,");
            strSql.Append("FApplyEmplNo=@FApplyEmplNo,");
            strSql.Append("FUpdEmplNo=@FUpdEmplNo");
            strSql.Append(" where FItemRequisitionApplyID=@FItemRequisitionApplyID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FApplyReason", SqlDbType.NVarChar,-1),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4),
					new SqlParameter("@FApplyDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FApplyEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FApplyReason;
            parameters[1].Value = model.FUpdDate;
            parameters[2].Value = model.FUpdIP;
            parameters[3].Value = model.FStatus;
            parameters[4].Value = model.FItemRequisitionApplyID;
            parameters[5].Value = model.FApplyDeptNo;
            parameters[6].Value = model.FApplyEmplNo;
            parameters[7].Value = model.FUpdEmplNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemRequisitionApplyEntry where FItemRequisitionApplyID=@FItemRequisitionApplyID");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4)
                                         };
            parameters3[0].Value = model.FItemRequisitionApplyID;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);

            foreach (Model.ItemRequisitionApplyEntry ItemRequisitionApplyEntryModel in ItemRequisitionApplyEntryList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_ItemRequisitionApplyEntry(");
                strSql.Append("FItemRequisitionApplyID,FApplyQty,FItemNo,FEntityNo)");
                strSql.Append(" values (");
                strSql.Append("@FItemRequisitionApplyID,@FApplyQty,@FItemNo,@FEntityNo)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4),
					new SqlParameter("@FApplyQty", SqlDbType.NVarChar,30),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
          
                parameters2[0].Value = model.FItemRequisitionApplyID;
                parameters2[1].Value = ItemRequisitionApplyEntryModel.FApplyQty;
                parameters2[2].Value = ItemRequisitionApplyEntryModel.FItemNo;
                parameters2[3].Value = ItemRequisitionApplyEntryModel.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

                        DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }

        public void Check(ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply model, List<Model.ItemRequisitionApplyEntry> ItemRequisitionApplyEntryList, List<Model.Item> ItemList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ItemRequisitionApply set ");
            strSql.Append("FStatus=@FStatus,");
            strSql.Append("FAuditDate=@FAuditDate,");
            strSql.Append("FAuditEmplNo=@FAuditEmplNo,");
            strSql.Append("FAuditIP=@FAuditIP");
            strSql.Append(" where FItemRequisitionApplyID=@FItemRequisitionApplyID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
                    new SqlParameter("@FAuditDate", SqlDbType.DateTime),
                    new SqlParameter("@FAuditEmplNo", SqlDbType.NVarChar,15),
                    new SqlParameter("@FAuditIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4)};
            parameters[0].Value = model.FStatus;
            parameters[1].Value = model.FAuditDate;
            parameters[2].Value = model.FAuditEmplNo;
            parameters[3].Value = model.FAuditIP;
            parameters[4].Value = model.FItemRequisitionApplyID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.ItemRequisitionApplyEntry ItemRequisitionApplyEntryModel in ItemRequisitionApplyEntryList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_ItemRequisitionApplyEntry set ");
                strSql.Append("FQty=@FQty,FPrice=@FPrice,FAmount=@FAmount ");
                strSql.Append(" where FItemRequisitionApplyEntryID=@FItemRequisitionApplyEntryID");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FQty", SqlDbType.Decimal,12),
					new SqlParameter("@FPrice", SqlDbType.Decimal,12),
					new SqlParameter("@FAmount", SqlDbType.Decimal,12),
					new SqlParameter("@FItemRequisitionApplyEntryID", SqlDbType.Int,4)};

                parameters2[0].Value = ItemRequisitionApplyEntryModel.FQty;
                parameters2[1].Value = ItemRequisitionApplyEntryModel.FPrice;
                parameters2[2].Value = ItemRequisitionApplyEntryModel.FAmount;
                parameters2[3].Value = ItemRequisitionApplyEntryModel.FItemRequisitionApplyEntryID;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            foreach (Model.Item Item in ItemList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Item set ");
                strSql.Append("FAmount=@FAmount,");
                strSql.Append("FQty=@FQty");
                strSql.Append(" where FItemNo=@FItemNo and FEntityNo=@FEntityNo");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                parameters2[0].Value = Item.FAmount;
                parameters2[1].Value = Item.FQty;
                parameters2[2].Value = Item.FItemNo;
                parameters2[3].Value = Item.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int FItemRequisitionApplyID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemRequisitionApply ");
            strSql.Append(" where FItemRequisitionApplyID=@FItemRequisitionApplyID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4)			};
            parameters[0].Value = FItemRequisitionApplyID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FItemRequisitionApplyIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemRequisitionApply ");
            strSql.Append(" where FItemRequisitionApplyID in (" + FItemRequisitionApplyIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply GetModel(int FItemRequisitionApplyID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FItemRequisitionApplyID,FEntityNo,FApplyDeptNo,FApplyEmplNo,FApplyReason,FApplyDate,FApplyIP,FUpdEmplNo,FUpdDate,FUpdIP,FStatus,FInsEmplNo,FAuditDate,FAuditEmplNo,FAuditIP from dbo.t_ItemRequisitionApply ");
            strSql.Append(" where FItemRequisitionApplyID=@FItemRequisitionApplyID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemRequisitionApplyID", SqlDbType.Int,4)			};
            parameters[0].Value = FItemRequisitionApplyID;

            ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply model = new ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply model = new ElingOA6_Admin_DSJCJ.Model.ItemRequisitionApply();
            if (row != null)
            {
                if (row["FItemRequisitionApplyID"] != null && row["FItemRequisitionApplyID"].ToString() != "")
                {
                    model.FItemRequisitionApplyID = int.Parse(row["FItemRequisitionApplyID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FApplyDeptNo"] != null)
                {
                    model.FApplyDeptNo = row["FApplyDeptNo"].ToString();
                }
                if (row["FApplyEmplNo"] != null)
                {
                    model.FApplyEmplNo = row["FApplyEmplNo"].ToString();
                }
                if (row["FApplyReason"] != null)
                {
                    model.FApplyReason = row["FApplyReason"].ToString();
                }
                if (row["FApplyDate"] != null && row["FApplyDate"].ToString() != "")
                {
                    model.FApplyDate = DateTime.Parse(row["FApplyDate"].ToString());
                }
                if (row["FApplyIP"] != null)
                {
                    model.FApplyIP = row["FApplyIP"].ToString();
                }
                if (row["FUpdEmplNo"] != null)
                {
                    model.FUpdEmplNo = row["FUpdEmplNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FInsEmplNo"] != null)
                {
                    model.FInsEmplNo = row["FInsEmplNo"].ToString();
                }
                if (row["FAuditDate"] != null && row["FAuditDate"].ToString() != "")
                {
                    model.FAuditDate = DateTime.Parse(row["FAuditDate"].ToString());
                }
                if (row["FAuditEmplNo"] != null)
                {
                    model.FAuditEmplNo = row["FAuditEmplNo"].ToString();
                }
                if (row["FAuditIP"] != null)
                {
                    model.FAuditIP = row["FAuditIP"].ToString();
                }

                if (row["FAuditIP"] != null)
                {
                    model.FAuditIP = row["FAuditIP"].ToString();
                }

                if (row.Table.Columns.Contains("Counts"))
                {
                    if (row["Counts"] != null)
                    {
                        model.Counts = row["Counts"].ToString();
                    }
                }

                if (row.Table.Columns.Contains("FDeptName"))
                {
                    if (row["FDeptName"] != null)
                    {
                        model.FDeptName = row["FDeptName"].ToString();
                    }
                }

                if (row.Table.Columns.Contains("FEmpName"))
                {
                    if (row["FEmpName"] != null)
                    {
                        model.FEmpName = row["FEmpName"].ToString();
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FItemRequisitionApplyID,FEntityNo,FApplyDeptNo,FApplyEmplNo,FApplyReason,FApplyDate,FApplyIP,FUpdEmplNo,FUpdDate,FUpdIP,FStatus,FInsEmplNo,FAuditDate,FAuditEmplNo,FAuditIP ");
            strSql.Append(" FROM dbo.t_ItemRequisitionApply ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

       
        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

